package com.souche.employment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.souche.employment.cache.RedisService;
import com.souche.employment.domain.UserDO;
import com.souche.employment.dto.UserDto;
import com.souche.employment.mapper.UserMapper;
import com.souche.employment.response.CustomException;
import com.souche.employment.response.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 
 * BaseController.java created at 2017年8月8日 下午1:11:20
 * @author Ray [email:rayinhangzhou@hotmail.com]
 * Revision History
 * Date            Programmer                   Notes
 * ----------------------------------------------------------
 **/

@RestController
@RequestMapping(value = "/user")
@Api(value = "/user", description = "帐号接口")
public class UserController {
	@Autowired
	private UserMapper userMapper;
    @Autowired
    private RedisService redisService;


    @RequestMapping(value="/getForList", method=RequestMethod.GET)
    @ApiOperation(value="获取帐号筛选列表")
    public DataResult getForList() {
        List<UserDO> list = userMapper.getForList();
        if (list==null) {
            list = new ArrayList<>();
        }
        return DataResult.success(list);
    }


	@RequestMapping(value="/search", method=RequestMethod.GET)
	@ApiOperation(value="搜索帐号")
	public DataResult search(
            @ApiParam(value = "邮箱") @RequestParam(required = false) String mail,
            @ApiParam(value = "类型") @RequestParam(required = false) String type,
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String sort) {
        sort = StringUtils.isBlank(sort)?"date_update desc":sort;
        PageHelper.startPage(pageNum, pageSize);
        List<UserDO> list = userMapper.search(mail, type, sort);
        List<UserDto> result = new ArrayList<>();
        if (list!=null) {
            list.stream().forEach(UserDO -> {
                UserDto dto = new UserDto(UserDO);
                dto.setLastLogin((String) redisService.hget(RedisService.KEY_LAST_LOGIN, UserDO.getId()));
                result.add(dto);
            });
        }
        PageInfo<UserDto> pageInfo = new PageInfo<>(result);
        return DataResult.success(pageInfo);
	}

	@RequestMapping(value = "/addOrEdit", method = RequestMethod.POST)
	@ApiOperation(value = "添加编辑帐号")
	public DataResult addOrEdit(
			@RequestParam(required = false) String id,
            @ApiParam(value = "邮箱", required = true) @RequestParam String mail,
            @ApiParam(value = "名称", required = true) @RequestParam String name,
			@ApiParam(value = "类型", required = true) @RequestParam String type) {
		if (StringUtils.isBlank(id)) {
			UserDO userDO = new UserDO();
			userDO.setId(mail);
			userDO.setName(name);
            userDO.setType(type);
            userDO.setDateCreate(Calendar.getInstance().getTime());
            userDO.setDateUpdate(Calendar.getInstance().getTime());
            userMapper.addUser(userDO);
            return DataResult.success(userDO);
        } else {
            UserDO userDO = userMapper.getUser(id);
            if (userDO==null) {
                throw CustomException.fail("该帐号不存在！", null);
            } else {
                userDO.setName(name);
                userDO.setType(type);
                userMapper.updateUser(userDO);
                return DataResult.success(userDO);
            }
        }
	}

    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ApiOperation(value="删除帐号")
    public DataResult delete(
            @RequestParam String id) {
        userMapper.deleteUser(id);
        return DataResult.success(id);
    }
	
	
}
